<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Api\Controller;

use Common\Lib\Util\ToolUtil;
use Admin\Model\AttachmentModel;

/**
 * @author fanyuliang
 */
class UploadController extends ApiController{
	
    public function index(){
    	$userId = I('user_id');
    	if (!$userId) {
	    	return $this->response('无效的请求参数',1);
    	}
    	$allowExts = array('jpg','jpeg','gif','png');
    	$maxSize = -1;
    	//附件类型
    	$type = I('type');
    	$savePath = C('ATTACHMENT_PATH').$type;
    	$image_crop = C('IMAGE_CROP');
    	$thumb = $image_crop[$type];
    	$result = ToolUtil::uploadFile($savePath,$maxSize,$allowExts,$thumb);
    	$uploadInfo = $this->handleAttache($result,$type,$userId,$thumb);
    	if (I('terminal') && I('terminal') == 1) {
    		return $this->response('success',200,$uploadInfo);
    	} else {
    		$result = array(
    			'msg' => 'success',
    			'code' => 200,
    			'data' => $uploadInfo
    		);
    		$this->ajaxReturn($result);
    	}
		
    }
    
    private function handleAttache($result,$type,$userId,$thumb){
    	$uploadInfo = array();
    	foreach ($result as $rs) {
    		$data = array();
    		$filePath = '/upload/'.$type.$rs['savename'];
    		$rs['filePath'] = $filePath;
    		$isImage = in_array($rs['extension'], array('jpg','jpeg','gif','png'));
//     		if ($thumb['prefix'] && in_array('small_', explode(',', $thumb['prefix'])) && $isImage) {
//     			//返回缩略图
//     			$savename = pathinfo($filePath,PATHINFO_BASENAME);
//     			$rs['filePath'] = str_replace($savename, 'small_'.$savename, $filePath);
//     		}
    		$attachmentModel = new AttachmentModel();
    		$data['type'] = $type;
    		$data['filename'] = $rs['name'];
    		$data['attachment'] = $filePath;
    		$data['filesize'] = $rs['size'];
    		$id = $attachmentModel->saveAttachement($data,$rs['extension'],$userId);
    		$uploadInfo[] = array_merge($rs,array('attachement_id' => $id));
    	}
    	return $uploadInfo;
    }
    /**
     * 等比缩放/裁剪中间区域
     */
    public function thumber() {
        $type = $_REQUEST['type'];
        $target = $_REQUEST['_target'];
        $ext = $_REQUEST['_ext'];
       if (!$target) {
            header('HTTP/1.1 404 Not Found');
            return;
        }

        $width = $_REQUEST['width'];
        $height = $_REQUEST['height'];

        //严格的审查条件
       if (!is_numeric($width) || $width < 10 || $width > 2000 || ((is_numeric($height) && ($height < 10 || $height > 2000))) || (!is_numeric($height) && $height != 'auto')) {
            header('HTTP/1.1 404 Not Found');
            return;
        }
        $cut = false;
        if (is_numeric($height)) {
            $cut = true;
        }
        $origin = str_replace('_' . $width . '_' . $height, '', $target . '.' . $ext);

        if (!file_exists('./upload/'.$type .'/' .$origin)) {
            header('HTTP/1.1 404 Not Found');
            return;
        }

        $info = getThumbImage($origin, $width, $height,$type ,$cut);

        $src = './upload/' . $info['src'];

        if ($ext == 'png') {
            header("Content-type: image/png");
            echo file_get_contents($src);
        } else if ($ext == 'jpg') {
            header("Content-type: image/jpg");
            echo file_get_contents($src);
        } else if ($ext == 'gif') {
            header("Content-type: image/gif");
            echo file_get_contents($src);
        }
    }
}

